package com.cf.demo.controller;

import com.cf.demo.mapper.UserMapper;
import com.cf.demo.pojo.Phone;
import com.cf.demo.pojo.User;
import com.cf.demo.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: chengfei
 * @Date: 2021/10/25
 */
@Controller
@RequestMapping("/helloworld")
public class HelloWorld {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/query")
    @ResponseBody
    public String query() {

        List<User> users = userMapper.selectAll();

        // 1 新增
        // Mapper接口只提供基础的增删改查  批量新增的需要使用 MySqlMapper 接口,该接口继承自 InsertListMapper 接口
        userMapper.insert(new User());
        userMapper.insertSelective(new User());
        userMapper.insertList(new ArrayList<User>());

        // 2 修改
        userMapper.updateByPrimaryKey(new User());
        userMapper.updateByPrimaryKeySelective(new User());
        userMapper.updateByExample(new User(), new Example(User.class));
        userMapper.updateByExampleSelective(new User(), new Example(User.class));

        // 3 删除
        userMapper.delete(new User());
        userMapper.deleteByPrimaryKey("id");
        userMapper.deleteByExample(new Example(User.class));

        // 4 查询
        userMapper.selectAll();
        userMapper.select(new User());
        userMapper.selectOne(new User());
        userMapper.selectByExample(new Example(User.class));

        System.out.println(users);

        return "<h1>Hello Wrold</h1>";
    }

    @GetMapping("/queryByCondition")
    @ResponseBody
    public String queryByCondition(@RequestParam("keyword") String keyword) {

        // 使用example来实现复杂关系的查询
        // 1 example可以查询自己需要的字段,并设置排序规则
        Example example = new Example(User.class);
        example.selectProperties("id", "phone", "icon")
                .orderBy("id")
                .desc();

        // 2 模糊查询需要自己拼百分号% (mybatis-plus不需要)
        example.createCriteria()
                .andLike("username", "%" + keyword + "%")
                .orEqualTo("phone", keyword);

        List<User> users = userMapper.selectByExample(example);
        System.out.println(users);

        return "<h1>Hello Wrold</h1>";
    }

    /**
     * 导出1  使用网上excel导出导入工具类  无需校验的导入导出
     *
     * @param response
     */
    @RequestMapping(value = "/export")
    public void exportExcel(HttpServletResponse response) throws IOException {

        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);

        List<Phone> phoneDTOList = new ArrayList<>();
        phoneDTOList.add(new Phone(6, date, format));
        phoneDTOList.add(new Phone(10, date, format));
        phoneDTOList.add(new Phone(20, date, format));


        ExcelUtils.exportExcel(phoneDTOList, "手机信息表", "手机信息", Phone.class, "手机信息", response);
    }



}
